<?php
namespace Home\Controller;
use Home\Controller\IndexController;
class UserOpenController extends IndexController {

    public function lst() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        //取出数据
        $model = D('SaleChance');
        $data = $model->search(1);
        //status变汉字状态
        $status_str = array();
        foreach ($data['data'] as $k => $v) {
            $st = $v['status'];
            $ss = '';
            if ($st == '1')
                $ss = '已指派';
            if ($st == '2')
                $ss = '开发成功';
            if ($st == '3')
                $ss = '开发失败';
            $status_str[$k] = $ss;
        }

        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
            'status_str' => $status_str,
        ));

        $this->display();
    }

    public function add() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        if (IS_POST) {
            $model = D('UserOpen');
            if ($model->create(I('post.'), 1)) {
                if ($id = $model->add()) {
                    $this->success('添加成功！', U('add?id=' . I('get.id') . '&p=' . $p));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $name = session('username');
        $time = time(); //时间戳
        $nowtime = date('Y-m-d H:i:s', $time); //生成带格式的日期

        $this->assign('name', $name);
        $this->assign('nowtime', $nowtime);

        $model = M('SaleChance');
        $data = $model->find($id);
        $this->assign('data', $data);
        //取出开发计划
        $sc_id = I('get.id');
        $uoModel = D('UserOpen');
        $uodata = $uoModel->where(array('sc_id' => array('eq', $sc_id)))->select();
        $this->assign('uodata', $uodata);

        $comeback = U('lst?p=' . I('get.p'));
        $this->assign('comeback', $comeback);
        $this->display();
    }
    public function haveLook() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        $model = M('SaleChance');
        $data = $model->find($id);
        $this->assign('data', $data);
        //取出开发计划
        $sc_id = I('get.id');
        $uoModel = D('UserOpen');
        $uodata = $uoModel->where(array('sc_id' => array('eq', $sc_id)))->select();
        $this->assign('uodata', $uodata);

        $comeback = U('lst?p=' . I('get.p'));
        $this->assign('comeback', $comeback);
        $this->display();
    }

    public function edit() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        if (IS_POST) {
            $model = D('UserOpen');
            if ($model->validate($rules)->create(I('post.'), 2)) {
                if ($model->save() !== FALSE) {
                    $this->success('修改成功！', U('add', array('id' => I('post.sc_id'),'p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
        }
    }

    public function toGood() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
        $id = I('get.id');
        $model = M('SaleChance');
        $data['id'] = $id;
        $data['status'] = 2;
        if ($model->save($data) !== FALSE) {
            $ccdata=array();
            $newdata=array();
            //客户信息插入客户表
            $scModel=M('sale_chance');
            $_data=$scModel->find($id);
            $newdata['sale_id'] = $id;
            $newdata['mname'] = $_data['give_name'];
            $newdata['name'] = $_data['client_name']; 
            $cmodel = M('Client');
            //有客户信息就不插入
            $ishave=$cmodel->where(array('name'=>array('eq',$newdata['name'])))->find();
            if($ishave==NULL)
            $cid=$cmodel->data($newdata)->add();
            $cid=$cid?$cid:$ishave['id'];
            //下单时间插入到客户流失表
            $rModel=M('Run');
            //有客户信息就不插入
            $ishave=$rModel->where(array('client_id'=>array('eq',$cid)))->find();
            if($ishave==NULL){
                $newdata['client_id'] = $cid;
                $newdata['give_name'] = $_data['give_name'];
                $newdata['last_time'] = time(); 
                $rModel->data($newdata)->add();
            }else {
                $newdata['id'] = $ishave['id'];
                $newdata['last_time'] = time(); 
                $rModel->data($newdata)-save();
            }
            
            
            //插入联系人表
            $ccModel=M('ClientContact');
            $ccdata['client_id']= $cid;
            $ccdata['name']= $_data['contact'];
            $ccdata['phone']= $_data['contact_phone'];
            $ccModel->data($ccdata)->add();
        
            $this->success('操作成功！', U('lst', array('p' => $p)));
            exit;
        }
        $this->error($model->getError());
    }

    public function stop() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
        $id = I('get.id');
        $model = M('SaleChance');
        $data['id'] = $id;
        $data['status'] = 3;
        if ($model->save($data) !== FALSE) {
            $this->success('操作成功！', U('lst', array('p' => $p)));
            exit;
        }
        $this->error($model->getError());
    }

    public function goOpen() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        $id = I('get.id');
        
        if (IS_POST) {
            $model = D('UserOpen');
            if ($model->validate($model->rules)->create(I('post.'), 2)) {
                if ($model->save() !== FALSE) {
                    $this->redirect('goOpen', array('id' => I('post.sc_id'),'p' => $p));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $name = session('username');
        $time = time(); //时间戳
        $nowtime = date('Y-m-d H:i:s', $time); //生成带格式的日期

        $this->assign('name', $name);
        $this->assign('nowtime', $nowtime);

        $model = M('SaleChance');
        $data = $model->find($id);
        $this->assign('data', $data);
        //取出开发计划
        $sc_id = I('get.id');
        $uoModel = D('UserOpen');
        $uodata = $uoModel->where(array('sc_id' => array('eq', $sc_id)))->select();
        $this->assign('uodata', $uodata);
        $this->assign('comeback', $comeback);
        $this->display();
    }

    public function delete() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $model = D('UserOpen');
        if ($model->delete(I('get.id')) !== FALSE) {
            $this->success('删除成功！', U('add', array('id' => I('get.gid'),'p' => $p)));
            exit;
        } else {
            $this->error($model->getError());
        }
    }
    /*****************************************ajax方法*********************************/
    public function ajaxAdd() {
        
        if (IS_POST) {
            $_data=array();
            $model = D('UserOpen');
            
                    $_data['time']=strtotime(I('post.time'));
                    $_data['plan']=I('post.plan');
                    $_data['p']=I('post.p');
                    $_data['id']=I('post.sc_id');
            if ($model->create(I('post.'), 1)) {
                if ($id = $model->add()) {
                    $data='<form id="demoform"  name="main_form" method="POST" action="/index.php/Home/UserOpen/edit" enctype="multipart/form-data">
                    <table class="tb" id="top" cellspacing="1" cellpadding="3" width="100%">
                    <tr>   
                           <input type="hidden" name="sc_id" value="'.$_data['id'].'"/>
                            <input type="hidden" name="id" value="'.$id.'"/>
                                <input type="hidden" name="p" value="'.$_data['p'].'"/> 
                                    <input type="hidden" name="time" value="'.$_data['time'].'"/>
                            <td class="alert alert-info">日期</td>
                            <td >'.I('post.time').'</td>
                            <td class="alert alert-info">计划项</td>
                            <td><input size="50" type="text"  name="plan" value="'.$_data['plan'].'" datatype="*1-100" errormsg="计划项最多100个字符！"/></td>
                <td><input type="submit" class="btn" id="Buttonb" value=" 保存 " />
                <a class="btn" href="/index.php/Home/UserOpen/delete/id/'.$id.'/p/'.$_data['p'].'/gid/'.$_data['id'].'"  onclick="return confirm('."'确定删除吗？'".');"> 删除 </a></td>       
                </tr>
              </table>
               </form>';
                    echo $data;
                    return $data;
                };
                return FALSE;     //<input type="button" class="Buttond"  value=" 删除 " onclick="return confirm('."'确定删除吗？'".');"/></td>
            }
        }    
            return FALSE;
    }
    public function ajaxDelete() {
        
        if (IS_POST) {
             $model = D('UserOpen');
            if ($model->delete(I('post.id')) !== FALSE) {
                echo 1;
                return TRUE;;
            } else {
                echo $model->getError();
                return FALSE;
            }
        };
    }
}
